package com.fg.ihr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fg.ihr.domain.Department;
import com.fg.ihr.domain.Location;
import com.fg.ihr.repository.DepartmentRepository;
import com.fg.ihr.repository.LocationRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    private static final Logger logger = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    DepartmentRepository departmentRepository;

    @GetMapping("/list")
    @ResponseBody
    public String getDeptList(@RequestParam(required=false) Integer uid
    ){
        List<Department> result = departmentRepository.findLocationList(uid);
        JSONArray list = JSONObject.parseArray(JSON.toJSONString(result));
        String departments = JSON.toJSONString(list);
        return departments;
    }
}
